package Reflection;

import Annotation.notation;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@notation(name = "Hello")
class MyClass {
    private int num;
    public String test;

    public int m1(int a, int b) {
        return a + b;
    }

    public void m2(int num) {
    }
}

public class Demo1 {
    @SneakyThrows
    @Test
    public void test() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
        // 获取某个指定类的信息
        // 首先获取Class的实例(MyClass类的类型对象)，该类的实例表示某个类运行时的信息
//        （获取类的名字、父类、实现的接口等。）
        Class<MyClass> sc = MyClass.class;
//        System.out.println(sc.getName());
//    通过实例对象打点 sc. 获取信息
        Method[] methods = sc.getDeclaredMethods();
//    for (Method method : methods) {
//        System.out.println(method);
//    }
//    System.out.println("__________________________");
//    for (Method method : methods) {
//        System.out.println(method.getName());
//    }
//        Field[] fields = sc.getFields();
//    for (Field field : fields) {
//        System.out.println(field.getName());
//    }
//        fields = sc.getFields();
//        for (Field field : fields) {
//            System.out.println(field.getName());
//        }
        MyClass myClass = new MyClass();
        Method m1 = sc.getDeclaredMethod("m1", int.class, int.class);
//       对象.方法（参数）
//       方法.invoke(对象，参数)
        System.out.println(m1.invoke(myClass, 2, 5));
        Method m2 = sc.getDeclaredMethod("m2", int.class);

        Field num = sc.getDeclaredField("num");
        num.setAccessible(true);
        num.set(myClass, 1);
        System.out.println(num.get(myClass));
//        判断该类是否被注解notation修饰
        if (sc.isAnnotationPresent(notation.class)) {

        }
        notation notation = sc.getDeclaredAnnotation(Annotation.notation.class);
        if (notation != null) {
            System.out.println(notation.name());
        }
        MyClass myClass1 = MyClass.class.newInstance();
    }


}
